# Create your views here.
from django.http import HttpResponse
from django.views.generic import list_detail

from rtve.core.models import Show
from rtve.feed.models import Feed

def search_wrapper(request):
    """
    Extract the articles based on the logged-in
    user information.
    """
    terms = request.REQUEST.get("terms", "")
    if terms != '':
        qs = Show.objects.filter(name__contains=terms.lower())
    else:
        qs = Show.objects.all()

    qs = qs.order_by('name')

    # Use the object_list view for the heavy lifting.
    return list_detail.object_list(request, queryset=qs, template_name="core/show_list_ajax.html")

def createFeed(request):
    shows = request.POST.getlist('checkedShows[]')
    (f,new) = Feed.objects.get_or_create(hash=",".join(shows))
    if (new):
        f.save()
    else:
        f.shows.clear()

    for show in shows:
        show = Show.objects.get(id=int(show)) 
        f.shows.add(show)
    f.save()
    return HttpResponse("http://%s" % request.META['SERVER_NAME'] + '/feeds/shows/' + str(f.id) + '/')

